/**
*
request({
method: 'post',
url: '/user/register',
 data: {
     username: usernameInput.value,
     password: passwordInput.value,
     email: emailInput.value
 },
 success(res) {
     var {error_code, msg} = res
     alert(msg)
     if(error_code === 0) {
         location.href = '/login.html'
     }
 }
})




request({
method: 'post',
url: '/user/login',
 data: {
     username: usernameInput.value,
     password: passwordInput.value
 },
 success: res => {
     var {error_code, msg} = res
     alert(msg)
     if(error_code === 0) {
         location.href = '/'
     }
 }
})


*
**/
// 封装发送ajax的get和post请求的函数
function request(obj) {
    if (obj.url === undefined) {
        throw new Error('请求地址不能为空！')
    }
    if (typeof obj.url != 'string') {
        throw new Error('请求地址不正确！')
    }
    if (obj.method === undefined) {
        obj.method = 'get'
    }
    if (obj.method.toLowerCase() != 'get' && obj.method.toLowerCase() != 'post') {
        throw new Error('请求方式必须是get或post')
    }
    if (obj.async === undefined) {
        obj.async = true
    }
    if (typeof obj.async != 'boolean') {
        throw new Error('async必须是布尔值！')
    }
    if (obj.data != undefined) {
        var data = ''
        if ({}.toString.call(obj.data) === '[object String]') {
            if (!obj.data.includes('=')) {
                throw new Error('字符串数据格式：键=值！')
            }
            data = obj.data
        } else if ({}.toString.call(obj.data) === '[object Object]') {
            var arr = []
            for (var key in obj.data) {
                arr.push(key + '=' + obj.data[key])
            }
            data = arr.join('&')
        } else {
            if (!obj.data.includes('=')) {
                throw new Error('数据必须是字符串或独享！')
            }
        }
        if (obj.method.toLowerCase() === 'get') {
            obj.url += '?' + data
        }
    }
    if (obj.success === undefined) {
        obj.success = () => {}
    }
    if (typeof obj.success != 'function') {
        throw new Error('success必须是函数')
    }
    if (obj.error === undefined) {
        obj.error = () => {}
    }
    if (typeof obj.error != 'function') {
        throw new Error('error必须是函数')
    }
    if (obj.dataType === undefined) {
        obj.dataType = 'json'
    }
    var xhr = new XMLHttpRequest
    xhr.open(obj.method, obj.url, obj.async)
    if (obj.headers != undefined) {
        if ({}.toString.call(obj.header) != '[object Object]') {
            throw new Error('headers头信息必须是对象！')
        }
        for (var key in obj.headers) {
            xhr.setRequestHeader(key, obj.headers[key])
        }
    }
    if (obj.method.toLowerCase() === 'post' && data != undefined) {
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        xhr.send(data)
    } else {
        xhr.send()
    }
    if (obj.async === true) {
        xhr.onreadystatechange = function () {
            console.log(xhr.readyState);
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    switch (obj.dataType) {
                        case 'json':
                            var res = xhr.responseText
                            res = JSON.parse(res)
                            break
                        case 'text':
                            var res = xhr.responseText
                            break
                        case 'xml':
                            var res = xhr.responseXML
                            break
                        default:
                            throw new Error('dataType必须是json或text或xml！')
                    }
                    obj.success(res)
                } else {
                    obj.error()
                }
            }
        }
    } else {
        switch (obj.dataType) {
            case 'json':
                var res = xhr.responseText
                res = JSON.parse(res)
                break
            case 'text':
                var res = xhr.responseText
                break
            case 'xml':
                var res = xhr.responseXML
                break
            default:
                throw new Error('dataType必须是json或text或xml！')
        }
        obj.success(res)
    }
}




